﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Westwind.Web;
using Westwind.Web.JsonSerializers;
using SqlShareAPI;

namespace WebQueryRole
{
    public partial class AJAXCallbackHandler : System.Web.UI.Page
    {
        private SSAPIFactory _factory;
        private UserSession _session;

        protected override void OnInit(EventArgs e)
        {
            _factory = CloudUtil.CreateSSAPIFactory();
            _session = WebAuthentication.IsLoggedIn(this, _factory);
            base.OnInit(e);
        }

        protected override void OnUnload(EventArgs e)
        {
            _factory.Close();
            base.OnUnload(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// The ajax library we use seems to want to substitute "null" when the initial string is empty ""
        /// </summary>
        /// <param name="str"></param>
        private void CleanProxyStr(ref string str)
        {
            if (str.Equals("null"))
            {
                str = "";
            }
        }

        // ajax calls
        [CallbackMethod]
        public string ajaxSaveQuery(string shortDesc, string longDesc, string sqlCode, bool isPublic)
        {
            CleanProxyStr(ref shortDesc);
            CleanProxyStr(ref longDesc);
            CleanProxyStr(ref sqlCode);

            if (shortDesc.Equals(""))
            {
                throw new Exception("invalid short description to update");
            }

            
            UserQuery query = _session.SaveUserQuery(shortDesc, longDesc, sqlCode, isPublic);
            JSONSerializer ser = new JSONSerializer();
            return ser.Serialize(query);
        }        

        // ajax calls
        [CallbackMethod]
        public string ajaxSaveTable(string tablename, string desc, bool isPublic)
        {

            CleanProxyStr(ref desc);

            UserTable table = _session.SaveUserTable(tablename, desc, isPublic);
            JSONSerializer ser = new JSONSerializer();
            return ser.Serialize(table);
        }

        [CallbackMethod]
        public string ajaxDeleteQuery(string shortDesc)
        {
            try
            {
                _session.DeleteUserQuery(shortDesc);
                return "true";
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception (ajaxDeleteSqlVar): " + e.Message);
                return "false";
            }
        }

        [CallbackMethod]
        public string ajaxDeleteTable(string tablename)
        {
            try
            {
                _session.DeleteUserTable(tablename);
                return "true";
            }
            catch (Exception e)
            {
                return "false";
            }
        }
    }
}